package com.nulstudio.cloud.gateway.util;

import com.nulstudio.cloud.gateway.exception.NulException;
import com.nulstudio.cloud.gateway.exception.NulExceptionConstants;
import org.springframework.lang.Nullable;

import java.util.regex.Pattern;

public final class FieldValidityAssertion {

    private static final Pattern EMAIL_PATTERN = Pattern.compile("[a-z0-9]+(?:[._\\\\-]*[a-z0-9])*@(?:[a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+");

    public static void assertEmail(@Nullable String email) {
        if (email == null || !EMAIL_PATTERN.matcher(email).matches())
            throw new NulException(NulExceptionConstants.INVALID_EMAIL);
    }

    private FieldValidityAssertion() {

    }
}
